const App = {
    mixins: [Query],//配置文件，路径/utils/mixins.js
    data() {
        return {
            page: {
                value1:'',
                PageNumber: 1, //页码
                RowAmount: 10,
                Condition: window.btoa(unescape(encodeURIComponent("%%"))),
            },
            Government_Agencies_SerialNumber:'',
            tree:{}
        }
    },
    mounted() {

    },
    methods: {
        check() {
            console.log( this.page.value1);
            if (this.page.value1) {
                this.page.Condition = window.btoa(unescape(encodeURIComponent("%" + this.page.value1 + "%")));
            } else {
                this.page.Condition = window.btoa(unescape(encodeURIComponent("%%")));
            }
            this.postgetList(this.page);
            this.Treedata()
        },
        //树的点击筛选事件
        handleNodeClick(data, node) {
            let arr = [];
            if (data.children) {
                this.Organization(data.children, arr);
            }
            this.Government_Agencies_SerialNumber = data.Government_Agencies_SerialNumber;
            this.level = node.level;
            let str = "";
            [data.Government_Agencies_SerialNumber, ...arr].forEach((item) => {
                if (str) {
                    str += "\r\nOR government_agencies.Parent_Number='" + item + "'";
                } else {
                    str = "\r\ngovernment_agencies.Parent_Number='" + item + "'";
                }
            });
            if (str) {
                str = "AND (" + str + ")";
            }
            this.page.ExtensionCondition = window.btoa(unescape(encodeURIComponent(str)));
            this.postgetList(this.page)
        },
        Organization(data, arr) {
            data.forEach((item) => {
                arr.push(item.Government_Agencies_SerialNumber);
                if (item.children && item.children.length > 0) {
                    this.Organization(item.children, arr);
                }
            });
        },
        Treedata() {
            try {
                let { TableName, NodeCode } = JSON.parse(document.getElementById("tree").getAttribute("data-options"));
                $.ajax({
                    headers: requestHeaders(),
                    url: `${mh}/generalBusiness/generalQueryForObtainingTrees`,
                    type: "GET",
                    data: {
                        TableName,
                        NodeCode
                    },
                    success: (data) => {
                        data.forEach(item => {
                            if (item.children&&item.children.length > 0) {
                                item.children.forEach(item => {
                                    if (item.children&&item.children.length > 0) {
                                        item.children.forEach(item=>{
                                            item.children = undefined;
                                        })
                                    }
                                })
                            }
                        })
                        data = this.Recursion(data)
                        this.treeData = data; // 直接使用 this
                    },
                    complete: (XMLHttpRequest) => {
                        requestPretreatment(XMLHttpRequest);
                    },
                    dataType: "json",
                });
            } catch (error) {

            }
        },
        handleDelete1(data) {
            this.$confirm('该操作将删除此条数据,是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(() => {
                let { TableName, primary_key } = JSON.parse(document.getElementById("table").getAttribute("data-options"));
                $.ajax({
                    headers: requestHeaders(),
                    url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
                    method: "post",
                    data: {
                        Data: TableName,
                        Condition: primary_key + "='" + data[primary_key] + "'"
                    },
                    success: (res) => {
                        if (res.length == 1) {
                            res[0].Row_Status = '1';
                            let lJSONObject_DataSet = new Object();
                            let lJSONObject_Table = new Object();
                            lJSONObject_Table[TableName] = res;
                            lJSONObject_DataSet['Modify'] = lJSONObject_Table;
                            let Operation_Main = JSON.stringify(deepClone(lJSONObject_DataSet));
                            if (!lJSONObject_DataSet['Add']) {
                                lJSONObject_DataSet['Add'] = new Object();
                            }
                            lJSONObject_DataSet['Add']['base_operation_log'] = [{
                                Operation_Type: '0',
                                Operation_Path: this.relativePath,
                                Operation_Main,
                                Operation_Interface: "generalSave",
                                Operation_IP: getIpAddress()
                            }]
                            submitSave(lJSONObject_DataSet,(res)=>{
                                if (res.Status == 'success') {
                                    this.Treedata();
                                } else {
                                    this.$message({
                                        type: 'error',
                                        message: '删除失败'
                                    });
                                }
                            })
                            // $.ajax({
                            //     headers: requestHeaders(),
                            //     url: `${mh}/generalBusiness/generalSave`,
                            //     method: "post",
                            //     data: {
                            //         Data: JSON.stringify(lJSONObject_DataSet),
                            //     },
                            //     success: (res) => {
                            //         if (res.Status == 'success') {
                            //             this.Treedata();
                            //         } else {
                            //             this.$message({
                            //                 type: 'error',
                            //                 message: '删除失败'
                            //             });
                            //         }
                            //     },
                            //     complete: (XMLHttpRequest) => {
                            //         requestPretreatment(XMLHttpRequest);
                            //     },
                            // });
                        } else {
                            this.$message({
                                type: 'error',
                                message: '删除失败'
                            });
                        }

                    },
                    complete: (XMLHttpRequest) => {
                        requestPretreatment(XMLHttpRequest);
                    },
                });
            }).catch(() => { });
        },
        personalAdd() {
            window.location.href = './add.html?action=Add' + (this.Government_Agencies_SerialNumber ? '&nodeKey=' + this.Government_Agencies_SerialNumber + "&level="+ this.level : '')
        },
        handleAdd(row) {
            window.location.href = './add.html?action=Add&level=' + row.Agensies_Level + '&nodeKey=' + row.Government_Agencies_SerialNumber
        },
        handleDelete(data,column) {
            console.log(this.tree);
            console.log(this.$refs.multipleTable);
            this.$confirm('该操作将删除此条数据,是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            }).then(() => {
                let { TableName, primary_key } = JSON.parse(document.getElementById("table").getAttribute("data-options"));
                $.ajax({
                    headers: requestHeaders(),
                    url: `${mh}/generalBusiness/generalQueryByTableNameWithOriginal`,
                    method: "post",
                    data: {
                        Data: TableName,
                        Condition: primary_key + "='" + data[primary_key] + "'"
                    },
                    success: (res) => {
                        if (res.length == 1) {
                            res[0].Row_Status = '1';
                            let lJSONObject_DataSet = new Object();
                            let lJSONObject_Table = new Object();
                            lJSONObject_Table[TableName] = res;
                            lJSONObject_DataSet['Modify'] = lJSONObject_Table;
                            let Operation_Main = JSON.stringify(deepClone(lJSONObject_DataSet));
                            if (!lJSONObject_DataSet['Add']) {
                                lJSONObject_DataSet['Add'] = new Object();
                            }
                            lJSONObject_DataSet['Add']['base_operation_log'] = [{
                                Operation_Type: '0',
                                Operation_Path: this.relativePath,
                                Operation_Main,
                                Operation_Interface: "generalSave",
                                Operation_IP: getIpAddress()
                            }]
                            submitSave(lJSONObject_DataSet,(res)=>{
                                if (res.Status == 'success') {
                                    this.Tabletdata = [];
                                    this.loading = true;
                                    this.postgetList(this.page);
                                } else {
                                    this.$message({
                                        type: 'error',
                                        message: '删除失败'
                                    });
                                }
                            })
                            // $.ajax({
                            //     headers: requestHeaders(),
                            //     url: `${mh}/generalBusiness/generalSave`,
                            //     method: "post",
                            //     data: {
                            //         Data: JSON.stringify(lJSONObject_DataSet),
                            //     },
                            //     success: (res) => {
                            //         if (res.Status == 'success') {
                            //             this.Tabletdata = [];
                            //             this.loading = true;
                            //             this.postgetList(this.page);
                            //         } else {
                            //             this.$message({
                            //                 type: 'error',
                            //                 message: '删除失败'
                            //             });
                            //         }
                            //     },
                            //     complete: (XMLHttpRequest) => {
                            //         requestPretreatment(XMLHttpRequest);
                            //     },
                            // });
                        } else {
                            this.$message({
                                type: 'error',
                                message: '删除失败'
                            });
                        }

                    },
                    complete: (XMLHttpRequest) => {
                        requestPretreatment(XMLHttpRequest);
                    },
                });
            }).catch(() => { });
        },
        Recursion(lArray_TreeData) {
            lArray_TreeData.forEach(lJSONObject_Record => {//1级
                //获取对象中子节点
                let lJSONArray_Children = lJSONObject_Record.children;
                if (!lJSONArray_Children) {//
                    let ls_ModleName = lJSONObject_Record.Agensies_Name;
                    if (!ls_ModleName.includes(this.page.value1)) {
                        lArray_TreeData = lArray_TreeData.filter(i => i.Government_Agencies_SerialNumber != lJSONObject_Record.Government_Agencies_SerialNumber)
                    }
                } else {
                    lJSONArray_Children = this.Recursion(lJSONArray_Children);
                    //判断子节点长度，若子节点长度大于0则处理好的子节点更新
                    if (lJSONArray_Children.length > 0) {
                        lJSONObject_Record.children = lJSONArray_Children
                    } else {//将该子节点删除
                        lArray_TreeData = lArray_TreeData.filter(i => i.Government_Agencies_SerialNumber != lJSONObject_Record.Government_Agencies_SerialNumber);
                    }
                }
            })
            return lArray_TreeData;
        },
        configTree(data,str){
            let { primary_key } = JSON.parse(document.getElementById("table").getAttribute("data-options"));
            data.forEach(row=>{
                if (row[primary_key] == str) {
                    console.log(row);
                } else if (row.children && row.children.length > 0) {
                    this.configTree(row.children, str);
                }
            })
        },
        load(tree, treeNode, resolve) {
            $.ajax({
                headers: requestHeaders(),
                url: `${mh}/generalBusiness/generalQueryBySQLSerialNumber`,
                method: "post",
                data: {
                    Model_Url: this.relativePath,
                    Condition: window.btoa(unescape(encodeURIComponent("%%"))),
                    ExtensionCondition: window.btoa(unescape(encodeURIComponent("AND Parent_Number='" + tree.Government_Agencies_SerialNumber + "'")))
                },
                success: (data) => {
                    data.forEach(item => {
                        item.children = [];
                        item.hasChildren = true;
                    })
                    this.tree[tree.Government_Agencies_SerialNumber] = tree;
                    resolve(data)
                },
                complete: (XMLHttpRequest) => {
                    requestPretreatment(XMLHttpRequest);
                },
            });

        }
    },
};

const { createApp } = Vue;
const app = createApp(App);
//这种方式是引入全部图标，无需单个引入
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component);
}
const ElMessage = ElementPlus.ElMessage;
app.use(ElementPlus, { locale: zhCn });
app.mount("#app");
